using System;
using System.IO;
using FireStarterModeller.BO;
using Habanero.Base;

namespace FireStarter.CodeGeneration
{
    public class WinProjectGenerator : ProjectGenerator
    {
        public override Result Generate()
        {
            CheckRequiredPropertiesSet();
            //if (ModelSolution.LogicProjectName == null) throw new PropertyMissingException("LogicProjectName");
            if (ModelSolution.BOProjectName == null) throw new PropertyMissingException("BOProjectName");
            if (ModelSolution.UIWinProjectName == null) throw new PropertyMissingException("UIWinProjectName");


            Directory.CreateDirectory(ProjectPath);

            ProjectFileManager uiProjectFileManager = new ProjectFileManager(ModelSolution.UIWinProjectName, SolutionGenerator.CodeTemplate);
            WinProjectTemplateLoader winProjectTemplateLoader = new WinProjectTemplateLoader();

            string projectFileName = Path.Combine(ProjectPath, ProjectName + ".csproj");
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName, OutputType.WinExe, SolutionGenerator.CodeTemplate);
            string contents = "";

            if (String.IsNullOrEmpty(ModelSolution.UIProjectName))
            {
                contents = winProjectTemplateLoader.GetProgramForWinOnly(ProjectName, ProjectName, null, ModelSolution.ApplicationName,
                                                                  ModelSolution.BOProjectName);
                string programFileName = Utilities.CreateFile(ProjectPath, "Program.cs", contents);
                projectFileManager.AddCodeFile(programFileName);

            }
            else
            {
                contents = winProjectTemplateLoader.GetUiManagerWin(ProjectName, ModelSolution.UIProjectName,
                    ModelSolution.UIWinProjectName, ModelSolution.ApplicationName);
                string uiManagerFileName = Utilities.CreateFile(ProjectPath, "UIManagerWin.cs", contents);
                projectFileManager.AddCodeFile(uiManagerFileName);

                contents = winProjectTemplateLoader.GetProgramForDual(ProjectName, ProjectName, null, ModelSolution.ApplicationName,
                                                               ModelSolution.BOProjectName);
                string programFileName = Utilities.CreateFile(ProjectPath, "Program.cs", contents);
                projectFileManager.AddCodeFile(programFileName);
            }

            contents = winProjectTemplateLoader.GetAppConfig((DMSolution) SolutionGenerator.ModelSolution);
            string appConfigFileName = Utilities.CreateFile(ProjectPath, "App.config", contents);

            contents = winProjectTemplateLoader.GetAssemblyInfo(ProjectName);
            string assemblyInfoFileName = Utilities.CreateFile(ProjectPath, "Properties\\AssemblyInfo.cs", contents);
            
            projectFileManager.AddFile(appConfigFileName);
            projectFileManager.AddProjectReference(ModelSolution.UIProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.BOProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.LogicProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.UIWinProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.BO.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.DB.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Win.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "log4net.dll"));
            projectFileManager.AddSystemReference("System.Windows.Forms");
            projectFileManager.SaveProjectFile();
            return new Result(true);
        }
    }
}